/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vistas;

import Modelo.Funciones;
import javax.swing.DefaultListModel;
import Modelo.Servicios.*;

/**
 *
 * @author KIKE
 */
public class Servicios extends javax.swing.JInternalFrame {

    DefaultListModel modeloListServicios = new DefaultListModel();
    DefaultListModel modeloListPlanes = new DefaultListModel();

    /**
     * Creates new form Servicios
     */
    public Servicios() {
        initComponents();

        listServicios.setModel(modeloListServicios);
        listPlanes.setModel(modeloListPlanes);

        this.poblarServicios();
    }

    public void poblarServicios() {
        this.limpiar();

        modeloListServicios.removeAllElements();

        Modelo.Servicios ser = new Modelo.Servicios();
        for (Object[] servicio : ser.getServicios()) {
            modeloListServicios.addElement(servicio[1]);
        }

        this.desactivarCampos();
        listServicios.setSelectedIndex(0);
    }

    public void poblarPlanes(int id_servicio) {
        modeloListPlanes.removeAllElements();

        Modelo.Servicios ser = new Modelo.Servicios();

        for (Object[] plan : ser.getPlanesByServicio(id_servicio)) {
            modeloListPlanes.addElement(plan[0]);
        }

        btnAddPlan.setEnabled(true);
        listPlanes.setEnabled(true);

        btnEditPlan.setEnabled(false);
        btnDeletePlan.setEnabled(false);

        txtPlan.setText("");

        listPlanes.setSelectedIndex(0);
    }

    public void desactivarCampos() {
        btnDeletePlan.setEnabled(false);
        btnEditPlan.setEnabled(false);
        btnAddPlan.setEnabled(false);
        btnDeleteServicio.setEnabled(false);
        btnEditServicio.setEnabled(false);
        listPlanes.setEnabled(false);
    }

    public void activarCamposPlanes() {
        btnDeletePlan.setEnabled(true);
        btnEditPlan.setEnabled(true);
    }

    public void desactivarCamposPlanes() {
        btnDeletePlan.setEnabled(false);
        btnDeletePlan.setEnabled(false);
    }

    public void activarCamposServicios() {
        btnDeleteServicio.setEnabled(true);
        btnEditServicio.setEnabled(true);
    }

    public void limpiar() {
        modeloListServicios.removeAllElements();
        modeloListPlanes.removeAllElements();
        txtServicio.setText("");
        txtPlan.setText("");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listServicios = new javax.swing.JList();
        btnDeleteServicio = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnAddServicio = new javax.swing.JButton();
        btnEditServicio = new javax.swing.JButton();
        txtServicio = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listPlanes = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        btnAddPlan = new javax.swing.JButton();
        btnEditPlan = new javax.swing.JButton();
        txtPlan = new javax.swing.JTextField();
        btnDeletePlan = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Servicios");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Servicios", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N
        jPanel1.setFont(new java.awt.Font("Microsoft Tai Le", 0, 11)); // NOI18N

        listServicios.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listServicios.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listServiciosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listServicios);

        btnDeleteServicio.setText("Borrar");
        btnDeleteServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteServicioActionPerformed(evt);
            }
        });

        btnAddServicio.setText("Agregar");
        btnAddServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddServicioActionPerformed(evt);
            }
        });

        btnEditServicio.setText("Editar");
        btnEditServicio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditServicioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(txtServicio)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(btnAddServicio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEditServicio)
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addComponent(txtServicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddServicio)
                    .addComponent(btnEditServicio))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(88, 88, 88)
                        .addComponent(btnDeleteServicio)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeleteServicio)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Planes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Microsoft Tai Le", 1, 12))); // NOI18N
        jPanel2.setFont(new java.awt.Font("Microsoft Tai Le", 1, 11)); // NOI18N

        listPlanes.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listPlanes.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listPlanesValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(listPlanes);

        btnAddPlan.setText("Agregar");
        btnAddPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddPlanActionPerformed(evt);
            }
        });

        btnEditPlan.setText("Editar");
        btnEditPlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditPlanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtPlan)
                .addGap(3, 3, 3))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(37, Short.MAX_VALUE)
                .addComponent(btnAddPlan)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnEditPlan)
                .addGap(33, 33, 33))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(19, Short.MAX_VALUE)
                .addComponent(txtPlan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAddPlan)
                    .addComponent(btnEditPlan))
                .addContainerGap())
        );

        btnDeletePlan.setText("Borrar");
        btnDeletePlan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeletePlanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(btnDeletePlan)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnDeletePlan)
                .addGap(27, 27, 27)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnDeletePlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeletePlanActionPerformed
        // TODO add your handling code here:
        Modelo.Servicios ser = new Modelo.Servicios();
        if (listPlanes.getSelectedIndex() > -1) {
            int id_servicio = ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue()));
            int id_plan = ser.getIdPlanByNombreYServicio(String.valueOf(listPlanes.getSelectedValue()), id_servicio);
            ser.deletePlan(id_plan);
            this.poblarPlanes(id_servicio);
        }
    }//GEN-LAST:event_btnDeletePlanActionPerformed

    private void btnAddServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddServicioActionPerformed
        // TODO add your handling code here:
        if (txtServicio.getText().trim().length() != 0) {
            Modelo.Servicios ser = new Modelo.Servicios();
            if (ser.existeServicio(txtServicio.getText())) {
                Funciones.mensaje("El servicio ya existe");
            } else {
                ser.addServicio(txtServicio.getText());
                this.poblarServicios();
            }
        }
    }//GEN-LAST:event_btnAddServicioActionPerformed

    private void listServiciosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listServiciosValueChanged
        // TODO add your handling code here:
        if (listServicios.getSelectedIndex() > -1) {
            Modelo.Servicios ser = new Modelo.Servicios();
            this.activarCamposServicios();
            txtServicio.setText(String.valueOf(listServicios.getSelectedValue()));
            //poblar planes
            this.poblarPlanes(ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue())));
        }
    }//GEN-LAST:event_listServiciosValueChanged

    private void btnDeleteServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteServicioActionPerformed
        // TODO add your handling code here:
        Modelo.Servicios ser = new Modelo.Servicios();
        if (listServicios.getSelectedIndex() > -1) {
            if (ser.existeServicio(String.valueOf(listServicios.getSelectedValue()))) {
                ser.deleteServicio(ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue())));
            }
            this.poblarServicios();
        }
    }//GEN-LAST:event_btnDeleteServicioActionPerformed

    private void btnEditServicioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditServicioActionPerformed
        // TODO add your handling code here:
        if (txtServicio.getText().trim().length() != 0) {
            Modelo.Servicios ser = new Modelo.Servicios();
            if (ser.existeServicio(txtServicio.getText())) {
                ser.updateServicio(txtServicio.getText(), ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue())));
            }
            this.poblarServicios();
        }
    }//GEN-LAST:event_btnEditServicioActionPerformed

    private void listPlanesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listPlanesValueChanged
        // TODO add your handling code here:
        this.activarCamposPlanes();

        txtPlan.setText(String.valueOf(listPlanes.getSelectedValue()));
    }//GEN-LAST:event_listPlanesValueChanged

    private void btnAddPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddPlanActionPerformed
        // TODO add your handling code here:
        if (txtPlan.getText().trim().length() != 0) {
            Modelo.Servicios ser = new Modelo.Servicios();
            int id_servicio = ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue()));
            if (ser.addPlan(txtPlan.getText(), id_servicio)) {
                this.poblarPlanes(id_servicio);
            }
        }
    }//GEN-LAST:event_btnAddPlanActionPerformed

    private void btnEditPlanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditPlanActionPerformed
        // TODO add your handling code here:
        if (txtPlan.getText().trim().length() != 0) {
            Modelo.Servicios ser = new Modelo.Servicios();
            int id_servicio = ser.getIdServicioByNombre(String.valueOf(listServicios.getSelectedValue()));
            int id_plan = ser.getIdPlanByNombreYServicio(String.valueOf(listPlanes.getSelectedValue()), id_servicio);
            ser.updatePlan(txtPlan.getText(), id_plan, id_servicio);
            this.poblarPlanes(id_servicio);
            txtPlan.setText("");
        }
    }//GEN-LAST:event_btnEditPlanActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddPlan;
    private javax.swing.JButton btnAddServicio;
    private javax.swing.JButton btnDeletePlan;
    private javax.swing.JButton btnDeleteServicio;
    private javax.swing.JButton btnEditPlan;
    private javax.swing.JButton btnEditServicio;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listPlanes;
    private javax.swing.JList listServicios;
    private javax.swing.JTextField txtPlan;
    private javax.swing.JTextField txtServicio;
    // End of variables declaration//GEN-END:variables
}
